package com.ncloud.timetask.scheduler;

import com.ncloud.timetask.business.MoveHistoryBusi;
import com.ncloud.timetask.common.Constant;
import com.ncloud.timetask.dao.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by shiqiang on 2017/7/6.
 */
@Component
public class MoveHistoryTask {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MoveHistoryBusi moveHistoryBusi;
    @Autowired
    private BaseDao baseDao;
    @SuppressWarnings(value="unchecked")
    @Scheduled(cron="0 30 01 * * ?") //每分钟执行一次
    public void statusCheck() {
        char state= Constant.WORK_STATE_LOGISTICS;
        //完成间隔时间
        int time=3;
        logger.debug("每天执行一次。开始……");
        StringBuffer sql = new StringBuffer("select DISTINCT a.order_id as orderId  from bs_o_work a where a.finish_time<date_sub(now(),interval ");
        sql.append(time).append(" day)");
        // and a.work_state='3'
        sql.append(" and a.work_state='").append(state).append("'");
        logger.info("sql{}"+sql.toString());
        List<Map<String,Object>> list = baseDao.commonQuery(sql.toString());
        list.stream().map(map->map.get("orderId")).forEach(orderId ->{
            logger.debug("orderId="+orderId.toString());
            moveHistoryBusi.moveHistory(String.valueOf(state),Long.parseLong(orderId.toString()));
        });
        logger.debug("每天执行一次。结束。");
    }
}
